﻿using Platform.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PlatformManagement
{
    public partial class QuantityControlForm : DevComponents.DotNetBar.Metro.MetroForm
    {
        public QuantityControlForm()
        {
            InitializeComponent();
            dgv.AutoGenerateColumns = false;
        }

        private void QuantityControlForm_Load(object sender, EventArgs e)
        {
            Binding_Cbb();
        }


        //Đổ dữ liệu vào các combobox
        private  void Binding_Cbb()
        {
            //clArt.DataSource = cbbArNo.DataSource = Article.Objects.All;
            //clArt.DisplayMember = cbbArNo.DisplayMember = "ArtName";
            //clArt.ValueMember = cbbArNo.ValueMember = "ArtNo";

          cbbArNo.DataSource = Article.Objects.All;
          cbbArNo.DisplayMember = "ArtName";
           cbbArNo.ValueMember = "ArtNo";

          //clMk.DataSource=  cbbMarket.DataSource = Market.Objects.All;// Where(x => x.ID == WorkSession.Current.Market.ID);
          //clMk.DisplayMember=  cbbMarket.DisplayMember = "MarketName";
          //clMk.ValueMember=  cbbMarket.ValueMember = "Id";

         cbbMarket.DataSource = Market.Objects.Where(x => x.ID == WorkSession.Current.Market.ID).ToList();
           cbbMarket.DisplayMember = "MarketName";
         cbbMarket.ValueMember = "Id";

           //clStore.DataSource= cbbStoreNo.DataSource = Store.Objects.All;
           //clStore.DisplayMember= cbbStoreNo.DisplayMember = "StoreName";
           //clStore.ValueMember= cbbStoreNo.ValueMember = "StoreNo";

           cbbStoreNo.DataSource = Store.Objects.All;
           cbbStoreNo.DisplayMember = "StoreName";
            cbbStoreNo.ValueMember = "StoreNo";

          //clSup.DataSource=  cbbSupNo.DataSource = Supplier.Objects.All;
          //clSup.DisplayMember=  cbbSupNo.DisplayMember = "Name";
          // clSup.ValueMember= cbbSupNo.ValueMember = "SupNo";


          cbbSupNo.DataSource = Supplier.Objects.All;
          cbbSupNo.DisplayMember = "Name";
          cbbSupNo.ValueMember = "SupNo";

            cbbOrderNo.DataSource = PickingList.Objects.All;
            cbbOrderNo.DisplayMember = "OrderNo";
            cbbOrderNo.ValueMember = "OrderNo";
        }


        private void btnCheck_Click(object sender, EventArgs e)
        {
            try
            {
                float n = 0;
                int k = 0;
                if (cbbOrderNo.Text == "" || txtOrderQty.Text == "" || txtPackage.Text == "" || cbbArNo.Text == "" || cbbMarket.Text == "" || cbbStoreNo.Text == "" || cbbSupNo.Text == "")
                {
                    MessageBox.Show("Nhập đầy đủ thông tin trước khi check");
                }
                else if(!float.TryParse(txtOrderQty.Text,out n))
                        MessageBox.Show("Khối lượng phải là số");
                else if (!float.TryParse(txtPackage.Text, out n))
                    MessageBox.Show("Số khay phải là số"); 
                else
                {
                    var a =
                        QuantityControl.Objects.Where(
                            x =>
                            x.OrderNo == cbbOrderNo.Text && x.OrderQty ==
                                                            float.Parse(txtOrderQty.Text) &&
                            x.Package == int.Parse(txtPackage.Text) &&
                            x.MarketId == int.Parse(cbbMarket.SelectedValue.ToString()) &&
                            x.StoreNo == cbbStoreNo.SelectedValue.ToString() && x.ArtNo == cbbArNo.SelectedValue.ToString() &&
                            x.SupNo == cbbSupNo.SelectedValue.ToString()).ToList();
                    if (a.Count >0)
                    {
                        if(a[0].OrderQty >a[0].Net && a[0].Net >0)
                            lblTT.Text = "Lô hàng cân chưa đủ";
                        else if(a[0].Net==0)
                            lblTT.Text = "Lô hàng chưa được cân";
                        dgv.DataSource = a;
                    }
                    else
                    {
                        lblTT.Text = "Lô hàng không tồn tại trong hệ thống";
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi " + ex.Message);
            }
           
        }
    }
}
